﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HFCAutoConfig
{
    public partial class Config : Form
    {
        public Config()
        {
            InitializeComponent();
        }

        private void Config_Load(object sender, EventArgs e)
        {
            string[] str = SerialPort.GetPortNames();
            if (str.Length == 0)
            {
                MessageBox.Show("未连接串口！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            foreach (string s in str)
            {
                cb_coms.Items.Add(s);
            }
            if (cb_coms.Items.Contains(Properties.Settings.Default.comname))
            {
                cb_coms.Text = Properties.Settings.Default.comname;
            }
            else
            {
                cb_coms.SelectedIndex = 0;
            }            
            cb_hz.Text = Properties.Settings.Default.com_hz.ToString();
            cb_data.Text = Properties.Settings.Default.com_d.ToString();
            cb_s.Text = Properties.Settings.Default.com_s.ToString();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_apply_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.comname = cb_coms.Text;
            Properties.Settings.Default.com_d = int.Parse(cb_data.Text);
            Properties.Settings.Default.com_hz = int.Parse(cb_hz.Text);
            Properties.Settings.Default.com_s = int.Parse(cb_s.Text);
            Properties.Settings.Default.Save();
            Form1.ME.SerialConnect();
            this.Close();
        }
    }
}
